package com.demo.reduce;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

import java.io.IOException;

/**
 * @author cxx
 * @create 2020-02-15 13:12
 **/
//                                                                    keyout代表输出的一条记录：指定行键
public class WordCountReduce extends TableReducer<Text, IntWritable, ImmutableBytesWritable> {

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        int total = 0;
        for(IntWritable v:values){
            total = total + v.get();
        }
        //输出：也是表中的一条记录
        //构造一个Put对象，把单词作为rowkey
        Put put = new Put (Bytes.toBytes (key.toString ()));
        put.addColumn (Bytes.toBytes ("content"),
                Bytes.toBytes("result"),
                Bytes.toBytes(String.valueOf(total)));
        context.write (new ImmutableBytesWritable(Bytes.toBytes (key.toString ())),put);
    }
}
